package com.cakeclient.Model;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import com.cakeclient.database.*;

import java.util.ArrayList;
import java.util.List;


public class Place implements IDatabasable {
    private Long idDB;
    private Long remoteID;
    private Address address;
    private GPSLocation location;

    private Place(DataSource dataSource, Cursor cursor) {
        idDB = cursor.getLong(cursor.getColumnIndexOrThrow(PlaceTable.ID));
        setRemoteID(cursor.getLong(cursor.getColumnIndexOrThrow(ProductTable.REMOTE_ID)));
        this.setAddress(new Address());
        address.setLine1(cursor.getString(cursor.getColumnIndexOrThrow(PlaceTable.ADDRESS_LINE_ONE)));
        address.setLine2(cursor.getString(cursor.getColumnIndexOrThrow(PlaceTable.ADDRESS_LINE_TWO)));
        address.setLine3(cursor.getString(cursor.getColumnIndexOrThrow(PlaceTable.ADDRESS_LINE_THREE)));
        address.setLine4(cursor.getString(cursor.getColumnIndexOrThrow(PlaceTable.ADDRESS_LINE_FOUR)));
        address.setCity(cursor.getString(cursor.getColumnIndexOrThrow(PlaceTable.CITY)));
        address.setPostalCode(cursor.getString(cursor.getColumnIndexOrThrow(PlaceTable.POSTALCODE)));
        address.setCountry(cursor.getString(cursor.getColumnIndexOrThrow(PlaceTable.COUNTRY)));
        this.setLocation(new GPSLocation(
                cursor.getFloat(cursor.getColumnIndexOrThrow(PlaceTable.LATITUDE)),
                cursor.getFloat(cursor.getColumnIndexOrThrow(PlaceTable.LONGITUDE))
        ));

    }

    public Place(com.cakeclient.clientsoap.contract.Address deliveryAddress) {
        this(new Address(deliveryAddress));
    }

    public Place(com.cakeclient.clientsoap.contract.GPSLocation location) {
        this(new GPSLocation(location));
    }

    public Place(Address address) {
        this.address = address;
    }

    public Place(GPSLocation location) {
        this.location = location;
    }

    public Long getRemoteID() {
        return remoteID;
    }

    private void setRemoteID(Long remoteID) {
        this.remoteID = remoteID;
    }

    public Long getIdDB() {
        return idDB;
    }

    public void setIdDB(Long idDB) {
        this.idDB = idDB;
    }


    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public GPSLocation getLocation() {
        return location;
    }

    public void setLocation(GPSLocation location) {
        this.location = location;
    }

    @Override
    public ContentValues getValues() {
        ContentValues values = new ContentValues();
        values.put(PaymentTypeTable.REMOTE_ID, this.getRemoteID());
        if (this.getAddress() != null) {
            values.put(PlaceTable.ADDRESS_LINE_ONE, this.getAddress().getLine1());
            values.put(PlaceTable.ADDRESS_LINE_TWO, this.getAddress().getLine2());
            values.put(PlaceTable.ADDRESS_LINE_THREE, this.getAddress().getLine3());
            values.put(PlaceTable.ADDRESS_LINE_FOUR, this.getAddress().getLine4());
            values.put(PlaceTable.CITY, this.getAddress().getCity());
            values.put(PlaceTable.POSTALCODE, this.getAddress().getPostalCode());
            values.put(PlaceTable.COUNTRY, this.getAddress().getCountry());
        } else {
            values.putNull(PlaceTable.ADDRESS_LINE_ONE);
            values.putNull(PlaceTable.ADDRESS_LINE_TWO);
            values.putNull(PlaceTable.ADDRESS_LINE_THREE);
            values.putNull(PlaceTable.ADDRESS_LINE_FOUR);
            values.putNull(PlaceTable.CITY);
            values.putNull(PlaceTable.POSTALCODE);
            values.putNull(PlaceTable.COUNTRY);
        }
        if (this.getLocation() != null) {
            values.put(PlaceTable.LONGITUDE, this.getLocation().getLongitude());
            values.put(PlaceTable.LATITUDE, this.getLocation().getLatitude());
        } else {
            values.putNull(PlaceTable.LONGITUDE);
            values.putNull(PlaceTable.LATITUDE);
        }
        return values;
    }

    @Override
    public void saveToDatabase(Context context) {
        //Protection anti-doublons
        Place bis = null;
        if (this.getIdDB() == null && this.getRemoteID() != null && (bis = Place.getOneFromRemoteID(context, this.getRemoteID())) != null) {
            this.setIdDB(bis.getIdDB());
        }

        DataSource dataSource = new DataSource(context);
        dataSource.open();
        if (this.getIdDB() == null) {
            dataSource.insert(this, PlaceTable.getInstance());
        } else {
            dataSource.update(this, PlaceTable.getInstance());
        }
        dataSource.close();
    }

    @Override
    public void removeFromDatabase(Context context) {
        if (this.getIdDB() != null) {
            DataSource dataSource = new DataSource(context);
            dataSource.open();
            dataSource.delete(this, PlaceTable.getInstance());
            dataSource.close();
        }
    }


    public static Place getOne(Context context, Long idDB) {
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchOne(idDB, PlaceTable.getInstance());
        cursor.moveToFirst();
        Place item = null;
        while (!cursor.isAfterLast()) {
            item = new Place(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        dataSource.close();
        return item;
    }

    public static Place getOneFromRemoteID(Context context, Long idDB) {
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchOneFromRemoteID(idDB, PlaceTable.getInstance());
        cursor.moveToFirst();
        Place item = null;
        while (!cursor.isAfterLast()) {
            item = new Place(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        dataSource.close();
        return item;
    }

    public static Place getOne(DataSource dataSource, long idDB) {
        Cursor cursor = dataSource.fetchOne(idDB, PlaceTable.getInstance());
        cursor.moveToFirst();
        Place item = null;
        while (!cursor.isAfterLast()) {
            item = new Place(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        ;
        return item;

    }

    static List<Place> getAll(Context context) {
        List<Place> result = new ArrayList<Place>();
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchAll(PlaceTable.getInstance());
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            result.add(new Place(dataSource, cursor));
            cursor.moveToNext();
        }
        dataSource.close();
        return result;
    }


}
